package com.cs307.redcross.client;

import java.io.Serializable;
import java.util.Date;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;

public class RecurrenceDialogBox extends DialogBox implements Serializable {
	private static final long serialVersionUID = 6425968222429030603L;	
	private String recurrence = "";
	private Date startDate;
	
	Label lblRepeats = new Label("Repeats:");
	Label repeatsLabel;
	Label lblStartsOn;
	Label lblEnds;
	Label lblSummary;
	ListBox repeatsCombobox;
	ListBox listBox;
	CheckBox sundayCheckbox;
	CheckBox mondayCheckbox;
	CheckBox tuesdayCheckbox;
	CheckBox wednesdayCheckbox;
	CheckBox thursdayCheckbox;
	CheckBox fridayCheckbox;
	CheckBox saturdayCheckbox;
	TextBox startDateTextBox;
	RadioButton endsNeverRadioButton;
	RadioButton endsAfterRadioButton;
	TextBox afterOcurrencesTextbox;
	Label lblOccurrences;
	RadioButton endsOnRadioButton;
	TextBox onOccurrencesTextBox;
	RadioButton dayOfMonthRadioButton;
	RadioButton dayOfWeekRadioButton;
	Label summaryLabel;
	
	public RecurrenceDialogBox() {
		
	}
	
	/**
	 * @wbp.parser.constructor
	 */
	public RecurrenceDialogBox(Date startDate) {
		this.startDate = startDate;		
		this.setText("Recurrence");
		
		AbsolutePanel dailyPanel = new AbsolutePanel();
		setWidget(dailyPanel);
		dailyPanel.setSize("386px", "302px");
		
		lblRepeats = new Label("Repeats:");
		dailyPanel.add(lblRepeats, 49, 12);
		
		repeatsLabel = new Label("Repeats On:");
		dailyPanel.add(repeatsLabel, 28, 45);
		repeatsLabel.setSize("79px", "18px");
		
		lblStartsOn = new Label("Starts On:");
		dailyPanel.add(lblStartsOn, 41, 84);
		lblStartsOn.setSize("66px", "18px");
		
		lblEnds = new Label("Ends:");
		dailyPanel.add(lblEnds, 68, 119);
		lblEnds.setSize("36px", "18px");
		
		lblSummary = new Label("Summary:");
		dailyPanel.add(lblSummary, 44, 209);
		lblSummary.setSize("36px", "18px");
		
		repeatsCombobox = new ListBox();
		repeatsCombobox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				if (repeatsCombobox.getSelectedIndex() != -1) {
					if (repeatsCombobox.getSelectedIndex() == 0)
						showDaily();
					else if (repeatsCombobox.getSelectedIndex() == 1)
						showWeekly();
					else if (repeatsCombobox.getSelectedIndex() == 2)
						showMonthly();
					else if (repeatsCombobox.getSelectedIndex() == 3)
						showYearly();
				}
			}
		});
		repeatsCombobox.addItem("Daily");
		repeatsCombobox.addItem("Weekly");
		repeatsCombobox.addItem("Monthly");
		repeatsCombobox.addItem("Yearly");
		dailyPanel.add(repeatsCombobox, 107, 10);
		repeatsCombobox.setSize("121px", "24px");
		
		sundayCheckbox = new CheckBox("S");
		dailyPanel.add(sundayCheckbox, 107, 46);
		
		mondayCheckbox = new CheckBox("M");
		dailyPanel.add(mondayCheckbox, 142, 46);
		
		tuesdayCheckbox = new CheckBox("T");
		dailyPanel.add(tuesdayCheckbox, 177, 46);
		tuesdayCheckbox.setSize("29px", "20px");
		
		wednesdayCheckbox = new CheckBox("W");
		dailyPanel.add(wednesdayCheckbox, 212, 46);
		
		thursdayCheckbox = new CheckBox("R");
		dailyPanel.add(thursdayCheckbox, 247, 46);
		thursdayCheckbox.setSize("29px", "20px");
		
		fridayCheckbox = new CheckBox("F");
		dailyPanel.add(fridayCheckbox, 282, 46);
		fridayCheckbox.setSize("29px", "20px");
		
		saturdayCheckbox = new CheckBox("S");
		dailyPanel.add(saturdayCheckbox, 317, 46);
		saturdayCheckbox.setSize("29px", "20px");
		
		startDateTextBox = new TextBox();
		dailyPanel.add(startDateTextBox, 107, 82);
		startDateTextBox.setSize("134px", "16px");
		startDateTextBox.setText(DateTimeFormat.getFormat("MM/dd/yy").format(startDate));
		
		
		endsNeverRadioButton = new RadioButton("ends", "Never");
		endsNeverRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				afterOcurrencesTextbox.setEnabled(false);
				onOccurrencesTextBox.setEnabled(false);
			}
		});
		dailyPanel.add(endsNeverRadioButton, 107, 119);
		
		endsAfterRadioButton = new RadioButton("ends", "After");
		endsAfterRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				afterOcurrencesTextbox.setEnabled(true);
				onOccurrencesTextBox.setEnabled(false);
			}
		});
		dailyPanel.add(endsAfterRadioButton, 107, 145);
		
		afterOcurrencesTextbox = new TextBox();
		dailyPanel.add(afterOcurrencesTextbox, 162, 146);
		afterOcurrencesTextbox.setSize("44px", "12px");
		
		lblOccurrences = new Label("occurrences");
		dailyPanel.add(lblOccurrences, 219, 145);
		lblOccurrences.setSize("28px", "18px");
		
		endsOnRadioButton = new RadioButton("ends", "On");
		endsOnRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				afterOcurrencesTextbox.setEnabled(false);
				onOccurrencesTextBox.setEnabled(true);
			}
		});
		dailyPanel.add(endsOnRadioButton, 107, 171);
		endsOnRadioButton.setSize("48px", "20px");
		
		onOccurrencesTextBox = new TextBox();
		dailyPanel.add(onOccurrencesTextBox, 150, 173);
		onOccurrencesTextBox.setSize("85px", "12px");
		
		dayOfMonthRadioButton = new RadioButton("day of", "day of month");
		dailyPanel.add(dayOfMonthRadioButton, 107, 45);
		dayOfMonthRadioButton.setSize("99px", "20px");
		
		dayOfWeekRadioButton = new RadioButton("day of", "day of week");
		dailyPanel.add(dayOfWeekRadioButton, 212, 46);
		dayOfWeekRadioButton.setSize("99px", "20px");
		
		showDaily();
		afterOcurrencesTextbox.setEnabled(false);
		onOccurrencesTextBox.setEnabled(false);
		startDateTextBox.setEnabled(false);
		
		Button cancelButton = new Button("New button");
		cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				recurrence = null;
				((RecurrenceDialogBox)((DecoratorPanel)((AbsolutePanel)((Button)event.getSource()).getParent()).getParent()).getParent()).hide();
			}
		});
		cancelButton.setText("Cancel");
		dailyPanel.add(cancelButton, 317, 264);
		cancelButton.setSize("59px", "28px");
		
		Button saveButton = new Button("Save");
		saveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				saveRecurrence();
			}
		});
		dailyPanel.add(saveButton, 252, 264);
		saveButton.setSize("59px", "28px");
		
		summaryLabel = new Label("");
		dailyPanel.add(summaryLabel, 110, 209);
	}
	
	public RecurrenceDialogBox(Date startDate, String recurString) {
		this.startDate = startDate;		
		this.setText("Recurrence");
		
		AbsolutePanel dailyPanel = new AbsolutePanel();
		setWidget(dailyPanel);
		dailyPanel.setSize("386px", "302px");
		
		lblRepeats = new Label("Repeats:");
		dailyPanel.add(lblRepeats, 49, 12);
		
		repeatsLabel = new Label("Repeats On:");
		dailyPanel.add(repeatsLabel, 28, 45);
		repeatsLabel.setSize("79px", "18px");
		
		lblStartsOn = new Label("Starts On:");
		dailyPanel.add(lblStartsOn, 41, 84);
		lblStartsOn.setSize("66px", "18px");
		
		lblEnds = new Label("Ends:");
		dailyPanel.add(lblEnds, 68, 119);
		lblEnds.setSize("36px", "18px");
		
		lblSummary = new Label("Summary:");
		dailyPanel.add(lblSummary, 44, 209);
		lblSummary.setSize("36px", "18px");
		
		repeatsCombobox = new ListBox();
		repeatsCombobox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				if (repeatsCombobox.getSelectedIndex() != -1) {
					if (repeatsCombobox.getSelectedIndex() == 0)
						showDaily();
					else if (repeatsCombobox.getSelectedIndex() == 1)
						showWeekly();
					else if (repeatsCombobox.getSelectedIndex() == 2)
						showMonthly();
					else if (repeatsCombobox.getSelectedIndex() == 3)
						showYearly();
				}
			}
		});
		repeatsCombobox.addItem("Daily");
		repeatsCombobox.addItem("Weekly");
		repeatsCombobox.addItem("Monthly");
		repeatsCombobox.addItem("Yearly");
		dailyPanel.add(repeatsCombobox, 107, 10);
		repeatsCombobox.setSize("121px", "24px");
		
		sundayCheckbox = new CheckBox("S");
		dailyPanel.add(sundayCheckbox, 107, 46);
		
		mondayCheckbox = new CheckBox("M");
		dailyPanel.add(mondayCheckbox, 142, 46);
		
		tuesdayCheckbox = new CheckBox("T");
		dailyPanel.add(tuesdayCheckbox, 177, 46);
		tuesdayCheckbox.setSize("29px", "20px");
		
		wednesdayCheckbox = new CheckBox("W");
		dailyPanel.add(wednesdayCheckbox, 212, 46);
		
		thursdayCheckbox = new CheckBox("R");
		dailyPanel.add(thursdayCheckbox, 247, 46);
		thursdayCheckbox.setSize("29px", "20px");
		
		fridayCheckbox = new CheckBox("F");
		dailyPanel.add(fridayCheckbox, 282, 46);
		fridayCheckbox.setSize("29px", "20px");
		
		saturdayCheckbox = new CheckBox("S");
		dailyPanel.add(saturdayCheckbox, 317, 46);
		saturdayCheckbox.setSize("29px", "20px");
		
		startDateTextBox = new TextBox();
		dailyPanel.add(startDateTextBox, 107, 82);
		startDateTextBox.setSize("134px", "16px");
		startDateTextBox.setText(DateTimeFormat.getFormat("MM/dd/yy").format(startDate));
		
		
		endsNeverRadioButton = new RadioButton("ends", "Never");
		endsNeverRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				afterOcurrencesTextbox.setEnabled(false);
				onOccurrencesTextBox.setEnabled(false);
			}
		});
		dailyPanel.add(endsNeverRadioButton, 107, 119);
		
		endsAfterRadioButton = new RadioButton("ends", "After");
		endsAfterRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				afterOcurrencesTextbox.setEnabled(true);
				onOccurrencesTextBox.setEnabled(false);
			}
		});
		dailyPanel.add(endsAfterRadioButton, 107, 145);
		
		afterOcurrencesTextbox = new TextBox();
		dailyPanel.add(afterOcurrencesTextbox, 162, 146);
		afterOcurrencesTextbox.setSize("44px", "12px");
		
		lblOccurrences = new Label("occurrences");
		dailyPanel.add(lblOccurrences, 219, 145);
		lblOccurrences.setSize("28px", "18px");
		
		endsOnRadioButton = new RadioButton("ends", "On");
		endsOnRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				afterOcurrencesTextbox.setEnabled(false);
				onOccurrencesTextBox.setEnabled(true);
			}
		});
		dailyPanel.add(endsOnRadioButton, 107, 171);
		endsOnRadioButton.setSize("48px", "20px");
		
		onOccurrencesTextBox = new TextBox();
		dailyPanel.add(onOccurrencesTextBox, 150, 173);
		onOccurrencesTextBox.setSize("85px", "12px");
		
		dayOfMonthRadioButton = new RadioButton("day of", "day of month");
		dailyPanel.add(dayOfMonthRadioButton, 107, 45);
		dayOfMonthRadioButton.setSize("99px", "20px");
		
		dayOfWeekRadioButton = new RadioButton("day of", "day of week");
		dailyPanel.add(dayOfWeekRadioButton, 212, 46);
		dayOfWeekRadioButton.setSize("99px", "20px");
		
		showDaily();
		afterOcurrencesTextbox.setEnabled(false);
		onOccurrencesTextBox.setEnabled(false);
		startDateTextBox.setEnabled(false);
		
		Button cancelButton = new Button("New button");
		cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				recurrence = null;
				((RecurrenceDialogBox)((DecoratorPanel)((AbsolutePanel)((Button)event.getSource()).getParent()).getParent()).getParent()).hide();
			}
		});
		cancelButton.setText("Cancel");
		dailyPanel.add(cancelButton, 317, 264);
		cancelButton.setSize("59px", "28px");
		
		Button saveButton = new Button("Save");
		saveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				saveRecurrence();
			}
		});
		dailyPanel.add(saveButton, 252, 264);
		saveButton.setSize("59px", "28px");
		
		summaryLabel = new Label("");
		dailyPanel.add(summaryLabel, 110, 209);
	}
	
	private void showDaily() {
		
		repeatsLabel.setVisible(false);
		sundayCheckbox.setVisible(false);
		mondayCheckbox.setVisible(false);
		tuesdayCheckbox.setVisible(false);
		wednesdayCheckbox.setVisible(false);
		thursdayCheckbox.setVisible(false);
		fridayCheckbox.setVisible(false);
		saturdayCheckbox.setVisible(false);
		dayOfMonthRadioButton.setVisible(false);
		dayOfWeekRadioButton.setVisible(false);
	}
	
	private void showWeekly() {
		repeatsLabel.setVisible(true);
		repeatsLabel.setText("Repeats On:");
		sundayCheckbox.setVisible(true);
		mondayCheckbox.setVisible(true);
		tuesdayCheckbox.setVisible(true);
		wednesdayCheckbox.setVisible(true);
		thursdayCheckbox.setVisible(true);
		fridayCheckbox.setVisible(true);
		saturdayCheckbox.setVisible(true);
		dayOfMonthRadioButton.setVisible(false);
		dayOfWeekRadioButton.setVisible(false);
	}
	
	private void showMonthly() {
		repeatsLabel.setVisible(true);
		repeatsLabel.setText("Repeats By:");
		sundayCheckbox.setVisible(false);
		mondayCheckbox.setVisible(false);
		tuesdayCheckbox.setVisible(false);
		wednesdayCheckbox.setVisible(false);
		thursdayCheckbox.setVisible(false);
		fridayCheckbox.setVisible(false);
		saturdayCheckbox.setVisible(false);
		dayOfMonthRadioButton.setVisible(true);
		dayOfWeekRadioButton.setVisible(true);
	}
	
	private void showYearly() {
		repeatsLabel.setVisible(false);
		sundayCheckbox.setVisible(false);
		mondayCheckbox.setVisible(false);
		tuesdayCheckbox.setVisible(false);
		wednesdayCheckbox.setVisible(false);
		thursdayCheckbox.setVisible(false);
		fridayCheckbox.setVisible(false);
		saturdayCheckbox.setVisible(false);
		dayOfMonthRadioButton.setVisible(false);
		dayOfWeekRadioButton.setVisible(false);
	}
	
	private void saveRecurrence() {
		recurrence = "";
		
		if (repeatsCombobox.getSelectedIndex() == 0) {
			recurrence += "D:M:";
						
			if (endsNeverRadioButton.getValue()) {
				recurrence += "731";
			}
			else if (endsAfterRadioButton.getValue()) {
				try {
					recurrence += Integer.parseInt(afterOcurrencesTextbox.getText());
				}
				catch (NumberFormatException nfe) {
					recurrence += "5";
				}
			}
			else if (endsOnRadioButton.getValue()) {
				//TODO: add this to recurrence
			}
			else {
				Window.alert("Please specify when the recurrence should end.");
				recurrence = "";
				return;
			}
		}
		else if (repeatsCombobox.getSelectedIndex() == 1) {
			recurrence += "W:";
			
			boolean validInput = false;
			if (sundayCheckbox.getValue()) {
				recurrence += "U";
				validInput = true;
			}
			if (mondayCheckbox.getValue()) {
				recurrence += "M";
				validInput = true;
			}
			if (tuesdayCheckbox.getValue()) {
				recurrence += "T";
				validInput = true;
			}
			if (wednesdayCheckbox.getValue()) {
				recurrence += "W";
				validInput = true;
			}
			if (thursdayCheckbox.getValue()) {
				recurrence += "H";
				validInput = true;
			}
			if (fridayCheckbox.getValue()) {
				recurrence += "F";
				validInput = true;
			}
			if (saturdayCheckbox.getValue()) {
				recurrence += "S";
				validInput = true;
			}
			
			if (!validInput) {
				Window.alert("Please specify a day of the week for the recurrence.");
				recurrence = "";
				return;
			}
			recurrence += ":";
			
			if (endsNeverRadioButton.getValue()) {
				recurrence += "105";
			}
			else if (endsAfterRadioButton.getValue()) {
				try {
					recurrence += Integer.parseInt(afterOcurrencesTextbox.getText());
				}
				catch (NumberFormatException nfe) {
					recurrence += "5";
				}
			}
			else if (endsOnRadioButton.getValue()) {
				//TODO: add this to recurrence
			}
			else {
				Window.alert("Please specify when the recurrence should end.");
				recurrence = "";
				return;
			}
		}
		else if (repeatsCombobox.getSelectedIndex() == 2) {
			recurrence += "M:";
			
			if (dayOfMonthRadioButton.getValue()) {
				recurrence += DateTimeFormat.getFormat("dd").format(startDate) + ":";
			}
			else if (dayOfWeekRadioButton.getValue()) {
				int dayOfMonth;
				try{
					dayOfMonth = Integer.parseInt(DateTimeFormat.getFormat("dd").format(startDate));
				}
				catch (Exception e) {
					Window.alert("Error: Failed to read start date. Please close the Recurrence dialog box and try again.");
					recurrence = "";
					return;
				}
				int weekNum = dayOfMonth / 7;
				recurrence += "" + weekNum + ";"; 
				
				if (DateTimeFormat.getFormat("EEE").format(startDate).equals("Sun")) {
					recurrence += "U:";
				}
				else if (DateTimeFormat.getFormat("EEE").format(startDate).equals("Mon")) {
					recurrence += "M:";
				}
				else if (DateTimeFormat.getFormat("EEE").format(startDate).equals("Tue")) {
					recurrence += "T:";
				}
				else if (DateTimeFormat.getFormat("EEE").format(startDate).equals("Wed")) {
					recurrence += "W:";
				}
				else if (DateTimeFormat.getFormat("EEE").format(startDate).equals("Thu")) {
					recurrence += "H:";
				}
				else if (DateTimeFormat.getFormat("EEE").format(startDate).equals("Fri")) {
					recurrence += "F:";
				}
				else if (DateTimeFormat.getFormat("EEE").format(startDate).equals("Sat")) {
					recurrence += "S:";
				}
			}			
			else {
				Window.alert("Please specify when the recurrence should end.");
				recurrence = "";
				return;
			}
			
			if (endsNeverRadioButton.getValue()) {
				recurrence += "9999";
			}
			else if (endsAfterRadioButton.getValue()) {
				try {
					recurrence += Integer.parseInt(afterOcurrencesTextbox.getText());
				}
				catch (NumberFormatException nfe) {
					recurrence += "35";
				}
			}
			else if (endsOnRadioButton.getValue()) {
				//TODO: add this to recurrence
			}
			else {
				Window.alert("Please specify when the recurrence should end.");
				recurrence = "";
				return;
			}
		}
		
		else if (repeatsCombobox.getSelectedIndex() == 3) {
			recurrence += "Y:";
			
			try{
				recurrence += DateTimeFormat.getFormat("MM/dd").format(startDate) + ":";
			}
			catch (Exception e) {
				Window.alert("Error: Failed to read start date. Please close the Recurrence dialog box and try again.");
				recurrence = "";
				return;
			}
			
			if (endsNeverRadioButton.getValue()) {
				recurrence += "9999";
			}
			else if (endsAfterRadioButton.getValue()) {
				try {
					recurrence += Integer.parseInt(afterOcurrencesTextbox.getText());
				}
				catch (NumberFormatException nfe) {
					recurrence += "35";
				}
			}
			else if (endsOnRadioButton.getValue()) {
				//TODO: add this to recurrence
			}
			else {
				Window.alert("Please specify when the recurrence should end.");
				recurrence = "";
				return;
			}
		}
		summaryLabel.setText(run(recurrence));
	}
    public static String run( String rec ){
        
        String array[] = rec.split( ":" );

        String recurrenceString = "";
		String first = array[0].toUpperCase();
        String second = array[1].toUpperCase();
        String third = array[2].toUpperCase();

		if ( first.equals( "D") ){
			
			recurrenceString = "Will happen for " + third + " day(s)";
            
		}else if ( first.equals( "W" ) ){
            
			recurrenceString= "Will happen on ";
			for ( char c : second.toCharArray() ){					
				
				String day = "";
				if ( Character.toString(c).equals( "M" ) ){
					day = "Monday";
				}
				else if (Character.toString(c).equals( "T" ) ){
					day = "Tuesday";
				}
				else if ( Character.toString(c).equals( "W" ) ){
					day = "Wednesday";
				}
				else if ( Character.toString(c).equals( "H" ) ){
					day = "Thursday";
				}
				else if ( Character.toString(c).equals( "F" ) ){
					day = "Friday";
				}
				else if ( Character.toString(c).equals( "S" ) ){
					day = "Saturday";
				}
				else if ( Character.toString(c).equals( "U" ) ){
					day = "Sunday";		            
				}
				recurrenceString += day +" ";
			}
            
			recurrenceString+= "for " + third +" week(s)" ;
			
            
		}else if ( first.equals( "M" ) ){
            
            String[] array2 = second.split( ";" );
            
            int day;
            
			if ( array2.length == 1 ){
				String d = array2[0];
                
				recurrenceString = "Will occur on the " + d + " of every Month";
				day = Integer.parseInt( d );
				
			}else if ( array2.length == 2 ){
                
				day = Integer.parseInt( array2[0] );				
				
				String g = "";
				if ( array2[1].equals( "M" ) ){
					g = "Monday";
				}
				else if ( array2[1].equals( "T" ) ){
					g = "Tuesday";
				}
				else if ( array2[1].equals( "W" ) ){
					g = "Wednesday";
				}
				else if ( array2[1].equals( "H" ) ){
					g = "Thursday";
				}
				else if ( array2[1].equals( "F" ) ){
					g = "Friday";
				}
				else if ( array2[1].equals( "S" ) ){
					g = "Saturday";
				}
				else if ( array2[1].equals( "U" ) ){
					g = "Sunday";		            
				}
				
				recurrenceString ="Will occur on the " + day + " " + g + " of every month";
                
			}
			
			recurrenceString+= " " + third + " time(s)";
            
		}else if ( first.equals( "Y") ){

			recurrenceString = "Will happen yearly on " + second + " " + third + " time(s)";
            
		}else{}
        return recurrenceString;
	}
}
